package cn.weiyan.dtx.seatademo.bank1.server.impl;

import cn.weiyan.dtx.seatademo.bank1.dao.AccountInfoDao;
import cn.weiyan.dtx.seatademo.bank1.server.AccountInfoServer;
import cn.weiyan.dtx.seatademo.bank1.spring.Bank2Client;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author misterWei
 * @create 2020年02月14号:17点41分
 * @mailbox mynameisweiyan@gmail.com
 */
@Service
@Slf4j
public class AccountInfoServerImpl implements AccountInfoServer {

    @Autowired
    private AccountInfoDao accountInfoDao;

    // 远程调用服务
    @Autowired
    private Bank2Client bank2Client;

    // 开启全局注解, + 本地事务注解
    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional
    @Override
    public void updateAccountBalance(String accountNo, Double amount) {
       //张三账户  减去金额,把该金额添加到李四账户中
        accountInfoDao.updateAccountBalance("1",amount * -1);
        log.error(RootContext.getXID());
        String transfer = bank2Client.transfer(amount);
        if (transfer.equals("rallBack;")){
            throw new RuntimeException("exception.....");
        }

        // 此处测试 两个分支业务操作执行成功之后,最后执行出现异常
        if (amount == 2){
            throw   new RuntimeException("rallback_tranaction...");
        }
    }
}
